<template>
    <el-dialog :title="!majorData.majorId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="majorData" :rules="majorRule" ref="majorData" @keyup.enter.native="dataFormSubmit()"
            label-width="80px">
            <el-form-item label="专业名称" prop="majorName">
                <el-input v-model="majorData.majorName" placeholder="专业名称"></el-input>
            </el-form-item>

            <el-form-item label="所属学院" prop="deptId">
                <el-popover ref="parentName" placement="bottom-start" trigger="click">
                    <el-tree :data="deptList" :props="deptListTreeProps" node-key="deptId" ref="deptListTree"
                        @current-change="majortListTreeCurrentChangeHandle" :default-expand-all="true"
                        :highlight-current="true" :expand-on-click-node="false">
                    </el-tree>
                </el-popover>
                <el-input v-model="majorData.dept.deptName" v-popover:parentName :readonly="true" placeholder="点击选择学院"
                    class="menu-majorData"></el-input>
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { deptApi, majorApi } from '@/api/baseinfo';
import { clone, treeDataToList } from '@/utils';

export default {
    components: {},
    props: {},
    data() {
        return {
            visible: false,
            majorData: {
                majarId: '',
                majorName: '',
                deptId: '',
                dept: {
                    deptName: ''
                }
            },
            majorRule: {
                deptId: { required: true, message: '学院名称不能为空', trigger: 'blur' },
                majorName: [
                    { required: true, message: '专业名称不能为空', trigger: 'blur' }
                ]

            },
            deptList: [],
            deptListTreeProps: {
                label: 'deptName',
                children: 'list'
            }

        };
    },
    watch: {},
    computed: {},
    methods: {
        init(id) {

            deptApi.getdeptListTree().then(({ data }) => {

                let cloneData = clone(data)
                this.deptList = cloneData
            }).then(() => {
                this.$nextTick(() => {
                    this.majorData = this.$options.data().majorData
                    this.$refs['majorData'].clearValidate()
                })
                if (id) {
                    return Promise.resolve(majorApi.getMajorInfo(id))
                } else {
                    return Promise.resolve(false)
                }

            }).then((res) => {
                if (res) {
                    this.majorData = res.major
                }
                this.visible = true

            })






        },
        majortListTreeCurrentChangeHandle(data, node) {
            this.majorData.deptId = data.deptId
            this.majorData.dept.deptName = data.deptName
        },

        dataFormSubmit() {
            this.$refs['majorData'].validate((valid) => {
                if (valid) {
                    majorApi.saveOrUpdate(this.majorData, this.majorData.majorId ? 'update' : 'save').then(() => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refreshDataList')
                            }
                        })

                    })
                }
            })


        }
    },
    created() { },
    mounted() { }
};
</script>

<style lang="scss">

</style>

<style lang="scss" scoped>

</style>